//
// Created by jinyf on 2022/3/2.
//

#include "MNAudio.h"

void pcmBufferCallBack(SLAndroidSimpleBufferQueueItf bf, void *context);

MNAudio::MNAudio(MNPlaystatus *_pPlaystatus, int _sample_rate, MNCallJava *_callJava) {
    this->playstatus = _pPlaystatus;
    this->sample_rate = _sample_rate;
    this->callJava = _callJava;
    queue = new MNQueue(_pPlaystatus);
    //第一个2=16位是2个字节
    // 第二个2=双声道
    buffer = (uint8_t *) av_malloc(sample_rate * channel * bit);

    sampleBuffer = static_cast<SAMPLETYPE *>(malloc(sample_rate * channel * bit));
    soundTouch = new SoundTouch();
    soundTouch->setSampleRate(sample_rate);
    soundTouch->setChannels(channel);
//    speed  1.1   1.5  2.1
    soundTouch->setTempo(speed);
}

MNAudio::~MNAudio() {

}

void *initPlay(void *data) {
    MNAudio *wlAudio = static_cast<MNAudio *>(data);
    wlAudio->initOpenSLES();
    pthread_exit(&wlAudio->openelsInitThread);
}

void MNAudio::play() {
    //初始化opensles 耗时操作
    pthread_create(&openelsInitThread, NULL, initPlay, this);

}

void MNAudio::initOpenSLES() {


    SLresult result;
    result = slCreateEngine(&engineObject, 0, 0, 0, 0, 0);
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);

    //第二步，创建混音器
    //和上面一样   create->Realize->GetInterface
    const SLInterfaceID mids[1] = {SL_IID_ENVIRONMENTALREVERB};
    const SLboolean mreq[1] = {SL_BOOLEAN_FALSE};
    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, mids, mreq);
    (void) result;
    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
    (void) result;
    result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB,
                                              &outputMixEnvironmentalReverb);


    if (SL_RESULT_SUCCESS == result) {
        result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(
                outputMixEnvironmentalReverb, &reverbSettings);
        (void) result;
    }
    SLDataLocator_OutputMix outputMix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
    SLDataSink audioSnk = {&outputMix, 0};



    // 第三步，配置PCM格式信息
    SLDataLocator_AndroidSimpleBufferQueue android_queue = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
                                                            2};
    SLDataFormat_PCM pcm = {
            SL_DATAFORMAT_PCM,//播放pcm格式的数据
            static_cast<SLuint32>(channel),//2个声道（立体声）
            static_cast<SLuint32>(getCurrentSampleRateForOpensles(sample_rate)),//44100hz的频率
            SL_PCMSAMPLEFORMAT_FIXED_16,//位数 16位
            SL_PCMSAMPLEFORMAT_FIXED_16,//和位数一致就行
            SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,//立体声（前左前右）
            SL_BYTEORDER_LITTLEENDIAN//结束标志
    };
    SLDataSource slDataSource = {&android_queue, &pcm};


    //打开三个接口
    const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE, SL_IID_VOLUME, SL_IID_MUTESOLO};
    const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};

//    const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, SL_IID_MUTESOLO};
//    const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};

    (*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slDataSource, &audioSnk, 3,
                                       ids, req);
    //初始化播放器
    (*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE);

//    得到接口后调用  获取Player接口 //获取一个操作播放器暂停、恢复的句柄
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &pcmPlayerPlay);

    //    得到操作左右声道、立体声接口
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_MUTESOLO, &pcmMutePlay);

//    注册回调缓冲区 获取缓冲队列接口
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_BUFFERQUEUE, &pcmBufferQueue);

    //脱离于系统音量操作声音
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_VOLUME, &pcmVolumePlay);

    //缓冲接口回调
    (*pcmBufferQueue)->RegisterCallback(pcmBufferQueue, pcmBufferCallBack, this);
//    获取播放状态接口
    (*pcmPlayerPlay)->SetPlayState(pcmPlayerPlay, SL_PLAYSTATE_PLAYING);
    pcmBufferCallBack(pcmBufferQueue, this);
}


void pcmBufferCallBack(SLAndroidSimpleBufferQueueItf bf, void *context) {
    //没数据也会一直循环
    LOGE("-------->打印 pcmBufferCallBack ，内部循环回调");
    MNAudio *wlAudio = (MNAudio *) context;
    if (wlAudio != NULL) {

//        int buffersize = wlAudio->resampleAudio();
        int buffersize = wlAudio->getSoundTouchData();

        LOGE("-------->打印 内部循环回调：%d", buffersize);
        if (buffersize > 0) {
            //避免频繁回调java，控制1秒
            if (wlAudio->clock_time - wlAudio->last_time >= 1) {
                wlAudio->last_time = wlAudio->clock_time;
                wlAudio->clock_time = wlAudio->clock_time + buffersize /
                                                            (double) (wlAudio->sample_rate *
                                                                      wlAudio->channel *
                                                                      wlAudio->bit);
                wlAudio->callJava->onCallTimeInfo(CHILD_THREAD, wlAudio->clock_time,
                                                  wlAudio->duration);
            }
//            (*wlAudio->pcmBufferQueue)->Enqueue(wlAudio->pcmBufferQueue, (char *) wlAudio->buffer,
//                                                buffersize);

            (*wlAudio->pcmBufferQueue)->Enqueue(wlAudio->pcmBufferQueue,
                                                (char *) wlAudio->sampleBuffer,
                                                buffersize * wlAudio->channel * wlAudio->bit);
        }

    }
}

int MNAudio::resampleAudio(void **pcmbuf) {
    while (playstatus != NULL && !playstatus->exit) {

//        if (playstatus->seeking){
//            continue;
//        }

        //队列中会通过av_packet_ref 复制一份数据给这里的avPacket，然后队列中的avPacket就会被free掉
        //所以这里也需要av_packet_alloc()
        avPacket = av_packet_alloc();
        if (queue->getAvpacket(avPacket) != 0) {
            av_packet_free(&avPacket);
            av_free(avPacket);
            avPacket = NULL;
            continue;
        }
        ret = avcodec_send_packet(avCodecContext, avPacket);
        if (ret != 0) {
            av_packet_free(&avPacket);
            av_free(avPacket);
            avPacket = NULL;
            continue;
        }
        avFrame = av_frame_alloc();
        ret = avcodec_receive_frame(avCodecContext, avFrame);
        if (ret == 0) {

            if (avFrame->channels && avFrame->channel_layout == 0) {
                avFrame->channel_layout = av_get_default_channel_layout(avFrame->channels);
            } else if (avFrame->channels == 0 && avFrame->channel_layout > 0) {
                avFrame->channels = av_get_channel_layout_nb_channels(avFrame->channel_layout);
            }

            //转换器
            SwrContext *swr_ctx;

            swr_ctx = swr_alloc_set_opts(
                    NULL,
                    AV_CH_LAYOUT_STEREO,
                    AV_SAMPLE_FMT_S16,
                    avFrame->sample_rate,
                    avFrame->channel_layout,
                    (AVSampleFormat) avFrame->format,
                    avFrame->sample_rate,
                    NULL, NULL
            );
            if (!swr_ctx || swr_init(swr_ctx) < 0) {
                av_packet_free(&avPacket);
                av_free(avPacket);
                avPacket = NULL;
                av_frame_free(&avFrame);
                av_free(avFrame);
                avFrame = NULL;
                swr_free(&swr_ctx);
                continue;
            }

//            int nb = swr_convert(
//                    swr_ctx,
//                    &buffer,
//                    avFrame->nb_samples,
//                    (const uint8_t **) avFrame->data,
//                    avFrame->nb_samples);

            nb = swr_convert(
                    swr_ctx,
                    &buffer,
                    avFrame->nb_samples,
                    (const uint8_t **) avFrame->data,
                    avFrame->nb_samples);


            int out_channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);
            data_size = nb * out_channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);


            //avFrame->pt 序号
            //等价于：avFrame->pts* av_q2d(time_base);
            now_time = avFrame->pts * time_base.num / (double) time_base.den;

            if (now_time < clock_time) {
                now_time = clock_time;
            }
            //主时间，还要加上他的播放时间，在pcmBufferCallBack中计算
            clock_time = now_time;


            *pcmbuf = buffer;
//            if (LOG_DEBUG) {
//                LOGE("data_size is %d", data_size);
//            }
            av_packet_free(&avPacket);
            av_free(avPacket);
            avPacket = NULL;
            av_frame_free(&avFrame);
            av_free(avFrame);
            avFrame = NULL;
            swr_free(&swr_ctx);
            break;
        } else {
            av_packet_free(&avPacket);
            av_free(avPacket);
            avPacket = NULL;
            av_frame_free(&avFrame);
            av_free(avFrame);
            avFrame = NULL;
            continue;
        }
    }


    return data_size;
}


int MNAudio::getCurrentSampleRateForOpensles(int sample_rate) {
    int rate = 0;
    switch (sample_rate) {
        case 8000:
            rate = SL_SAMPLINGRATE_8;
            break;
        case 11025:
            rate = SL_SAMPLINGRATE_11_025;
            break;
        case 12000:
            rate = SL_SAMPLINGRATE_12;
            break;
        case 16000:
            rate = SL_SAMPLINGRATE_16;
            break;
        case 22050:
            rate = SL_SAMPLINGRATE_22_05;
            break;
        case 24000:
            rate = SL_SAMPLINGRATE_24;
            break;
        case 32000:
            rate = SL_SAMPLINGRATE_32;
            break;
        case 44100:
            rate = SL_SAMPLINGRATE_44_1;
            break;
        case 48000:
            rate = SL_SAMPLINGRATE_48;
            break;
        case 64000:
            rate = SL_SAMPLINGRATE_64;
            break;
        case 88200:
            rate = SL_SAMPLINGRATE_88_2;
            break;
        case 96000:
            rate = SL_SAMPLINGRATE_96;
            break;
        case 192000:
            rate = SL_SAMPLINGRATE_192;
            break;
        default:
            rate = SL_SAMPLINGRATE_44_1;
    }
    return rate;
}

void MNAudio::pause() {
    if (pcmPlayerPlay != NULL) {
        (*pcmPlayerPlay)->SetPlayState(pcmPlayerPlay, SL_PLAYSTATE_PAUSED);
    }
}

void MNAudio::resume() {
    if (pcmPlayerPlay != NULL) {
        (*pcmPlayerPlay)->SetPlayState(pcmPlayerPlay, SL_PLAYSTATE_PLAYING);
    }
}

void MNAudio::setMute(int mute) {
    LOGE(" 声道  接口%p", pcmMutePlay);
    if (pcmMutePlay == NULL) {
        return;
    }
    if (mute == 0)//right 右声道   0
    {
//        1
//        true代表关闭这个喇叭，false代表输出这个喇叭

        (*pcmMutePlay)->SetChannelMute(pcmMutePlay, 1, false);
        (*pcmMutePlay)->SetChannelMute(pcmMutePlay, 0, true);

    } else if (mute == 1)//left 左声道
    {
        (*pcmMutePlay)->SetChannelMute(pcmMutePlay, 1, true);
        (*pcmMutePlay)->SetChannelMute(pcmMutePlay, 0, false);
    } else if (mute == 2)//center
    {

        (*pcmMutePlay)->SetChannelMute(pcmMutePlay, 1, false);
        (*pcmMutePlay)->SetChannelMute(pcmMutePlay, 0, false);
    }
}


void MNAudio::setVolume(int percent) {
    if (pcmVolumePlay != NULL) {
        if (percent > 30) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -20);
        } else if (percent > 25) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -22);
        } else if (percent > 20) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -25);
        } else if (percent > 15) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -28);
        } else if (percent > 10) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -30);
        } else if (percent > 5) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -34);
        } else if (percent > 3) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -37);
        } else if (percent > 0) {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -40);
        } else {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -100);
        }
    }
}

void MNAudio::setSpeed(float d) {
    this->speed = d;
    if (soundTouch != NULL) {
        soundTouch->setTempo(d);
    }
}

void MNAudio::setPitch(float d) {
    this->pitch = d;
    if (soundTouch != NULL) {
        soundTouch->setPitch(d);
    }
}


int MNAudio::getSoundTouchData() {
//        重新整理波形  sountouch
//我们先取数据   pcm 的数据  就在  outbuffer
    while (playstatus != NULL && !playstatus->exit) {
        LOGE("------------------循环---------------------------finished %d", finished)
        out_buffer = NULL;
        if (finished) {
            finished = false;
            data_size = this->resampleAudio(reinterpret_cast<void **>(&out_buffer));
            if (data_size > 0) {
                for (int i = 0; i < data_size / 2 + 1; i++) {
                    //short  2个字节  pcm数据   ====波形
                    sampleBuffer[i] = (out_buffer[i * 2] | ((out_buffer[i * 2 + 1]) << 8));
                }
                //丢给sountouch   进行波的整理
                soundTouch->putSamples(sampleBuffer, nb);
                //接受一个新波 sampleBuffer
                num = soundTouch->receiveSamples(sampleBuffer, data_size / 2 / channel);
                LOGE("------------第一个num %d ", num);
            } else {
                soundTouch->flush();
            }
        }

        if (num == 0) {
            finished = true;
            continue;
        } else {
            if (out_buffer == NULL) {
                num = soundTouch->receiveSamples(sampleBuffer, data_size / 2 / channel);
                LOGE("------------第二个num %d ", num);
                if (num == 0) {
                    finished = true;
                    continue;
                }
            }
            LOGE("---------------- 结束1 -----------------------")
            return num;
        }

    }
    LOGE("---------------- 结束2 -----------------------")
    return 0;
}

void MNAudio::release() {
    if (queue != NULL) {
        delete (queue);
        queue = NULL;
    }
    if (pcmPlayerObject != NULL) {
        (*pcmPlayerObject)->Destroy(pcmPlayerObject);
        pcmPlayerObject = NULL;
        pcmPlayerPlay = NULL;
        pcmBufferQueue = NULL;
    }

    if (outputMixObject != NULL) {
        (*outputMixObject)->Destroy(outputMixObject);
        outputMixObject = NULL;
        outputMixEnvironmentalReverb = NULL;
    }

    if (engineObject != NULL) {
        (*engineObject)->Destroy(engineObject);
        engineObject = NULL;
        engineEngine = NULL;
    }

    if (buffer != NULL) {
        free(buffer);
        buffer = NULL;
    }

    if (avCodecContext != NULL) {
        avcodec_close(avCodecContext);
        avcodec_free_context(&avCodecContext);
        avCodecContext = NULL;
    }

    if (playstatus != NULL) {
        playstatus = NULL;
    }
    if (callJava != NULL) {
        callJava = NULL;
    }
}



